using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Power.Don;
using Power.Don.Repository;
using Power.Infrastructure.Db;




namespace Power.Infrastructrue.Repository
{
    public class RepositoryBase<T> : IRepository<T> where T:BaseEntity
    {

        private readonly PowerDbContext _db;

        private readonly  DbSet<T> _table;

        public RepositoryBase()
        {
            _db = new PowerDbContext();
            _table = _db.Set<T>();
        }

        public IQueryable<T> Table
        {
            get
            {
                return _table.AsNoTracking();
            }
        }

        public T GetById(Guid id)
        {
            var entity =
                _db
                    .Set<T>()
                    .AsQueryable()
                    .Where(x => x.Id == id)
                    .FirstOrDefault();
            return entity;
        }

        public void Add(T entity)
        {
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedAt = DateTime.Now;
            entity.UpdatedAt = DateTime.Now;
            entity.DisplayOrder = 0;
            entity.DisplayOrder = 0;
            _table.Add (entity);
            _db.SaveChanges();
        }

        public void Update(T entity)
        {
        }

        public void Delete(Guid id)
        {
        }
    }
    }